package com.example.helloworldspringboot.service;

import com.example.helloworldspringboot.dao.CategoryInterface;
import com.example.helloworldspringboot.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService implements CategoryInterface {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int addCategory(Category category) {
        return jdbcTemplate.update("insert into category values(null,?,?)",category.getName(),category.getStatus());
    }

    @Override
    public int deleteCategory(Category category) {
        return jdbcTemplate.update("delete from category where Id=?",category.getId());
    }

    @Override
    public int updateCategory(Category category) {
        return jdbcTemplate.update("update category set name=? where Id=?",category.getName(),category.getId());
    }

    @Override
    public Category queryCategory(Category category) {
        return jdbcTemplate.queryForObject("select * from category where Id="+category.getId(),new BeanPropertyRowMapper<Category>(Category.class));
    }

    @Override
    public List<Category> queryAllCategory() {
        return jdbcTemplate.query("select * from category",new BeanPropertyRowMapper<Category>(Category.class));
    }
}
